Import brl
Import diddy
Import gameglobals

Class RPGXML

	Field doc:XMLDocument
	Field gdata:Int[]
	
	Method LoadGameData:int()
	
		Local parser:XMLParser = New XMLParser
		Local xmlString:String = LoadString("myrpg.xml")		
		If Not xmlString
			AssertError("Cannot load tile data file myrpg.xml")
			Print "Why no rpg?"
		End
		doc = parser.ParseString(xmlString)
		
		'Get Root node
		Local rootnode:XMLElement = doc.Root
		For Local resourcenode:XMLElement = Eachin rootnode.Children
			Select resourcenode.Name
				
				Case "data"
					'load general game data
					GetGenData(resourcenode)
					Print "Load Game Data"
					
				Case "media"
					'load map data #use tiled format
					GetMedia(resourcenode)
				
			End Select
		Next
		Return 1	
	End
	
	
	Method GetMedia:Void(datanode:XMLElement)
	
		For Local mtype:XMLElement = Eachin datanode.Children
			Select mtype.Name
				
				Case "palettes"
					For Local palettesnode:XMLElement = Eachin mtype.Children
						Select palettesnode.Name
					
							Case "master"
								For Local palettenode:XMLElement = Eachin palettesnode.Children
								
									If MasterPalette = Null Then MasterPalette = New Palette256
									Local bytes:Int[] = DecodeBase64Bytes(palettenode.Value)
									For Local i% = 0 Until bytes.Length Step 3
										' little endian
										Local gid:Int[3] 
										gid[0] = bytes[i]
										gid[1] = bytes[i + 1]
										gid[2] = bytes[i + 2]
										'gid += bytes[i + 3] Shl 24
										
										'Local col:Int[] = MasterPalette.IntToRGBA( gid )
										'Print "MASTER COLOR: R "+gid[0]+" G "+gid[1]+" B "+gid[2]
										If i = 0 Then
											MasterPalette.col[i / 3] = 0
										Else
										
											MasterPalette.col[i / 3] = MasterPalette.ToARGB(255,gid[0],gid[1],gid[2])
										Endif
									Next								
								Next
								
							Case "game"
					
								Local pal:Palette256[256]'Int(palettesnode.GetAttribute("count"))]
								
								Local countp:Int=0
								For Local palettenode:XMLElement = Eachin palettesnode.Children
								
									If pal[Int(palettenode.GetAttribute("id"))] = Null Then pal[Int(palettenode.GetAttribute("id"))] = New Palette256
									
									
									Local csv:String[] = palettenode.Value.Split(",")
									For Local i% = 0 Until csv.Length Step 3
										Local gidr:Int = Int(csv[i].Trim())
										Local gidg:Int = Int(csv[i+1].Trim())
										Local gidb:Int = Int(csv[i+2].Trim())
										
										If countp = 0 Then
										
											'Print "Palette: R: "+gidr+" G: "+gidg+" B: "+gidb+" INT: "+pal[Int(palettenode.GetAttribute("id"))].ToARGB(255,gidr,gidg,gidb)
										
										Endif
										
										If i = 0 Then
											pal[Int(palettenode.GetAttribute("id"))].col[i/3] = 0
										Else
											pal[Int(palettenode.GetAttribute("id"))].col[i/3] = pal[Int(palettenode.GetAttribute("id"))].ToARGB(255,gidr,gidg,gidb)
										Endif
										'rv.cells[i] = tileMap.CreateCell(gid, i Mod rv.width, i / rv.width)
									Next										
									#rem
									Local bytes:Int[] = DecodeBase64Bytes(palettenode.Value)
									For Local i% = 0 Until bytes.Length Step 3
										' little endian
										Local gid:Int[3] 
										gid[0] = bytes[i]
										gid[1] = bytes[i + 1]
										gid[2] = bytes[i + 2]
										'Print "Palette "+(i/4)+" col "+gid
										'pal[countp].col[i / 4] = gid
										'Local col:Int[] = pal[countp].IntToRGBA( gid )
										'Print "MASTER COLOR: R "+col[0]+" G "+col[1]+" B "+col[2]+" A "+col[3]
										pal[countp].col[i / 3] = pal[countp].ToARGB(255,gid[0],gid[1],gid[2])										
									Next									
									#end
									countp = countp + 1
								Next
								Palettes = pal
					
					
						End Select
					Next
				
				Case "backdrops"
					Backdrops = New BackdropState
					Backdrops.SetBackdropArray(Int(mtype.GetAttribute("count")))
					For Local backdropnode:XMLElement = Eachin mtype.Children
						Local bd:Int = Int(backdropnode.GetAttribute("id"))
						Backdrops.backdrop[bd] = New BackdropData
						Backdrops.backdrop[bd].filename = backdropnode.GetAttribute("source")
						Backdrops.backdrop[bd].img = LoadImage(diddyGame.images.path + backdropnode.GetAttribute("source"))
					Next
				Case "sprites"
					
					For Local spritenode:XMLElement = Eachin mtype.Children
						
						Local tempi:Int = 0 '*****Use ids instead
						Select spritenode.Name
					
							Case "walkabout"
								Local tmpImage:Image
								Local ID:Int = Int(spritenode.GetAttribute("id"))
								WalkaboutSpr[ID] = New SpriteSet
								WalkaboutSpr[ID].anim = diddyGame.images.LoadAnim(spritenode.GetAttribute("fullset"), Int(spritenode.GetAttribute("w")), Int(spritenode.GetAttribute("h")), Int(spritenode.GetAttribute("frames")), tmpImage, false, False, "walkabout_"+ID, True)
								
								'Print "Loaded "+spritenode.GetAttribute("fullset")
								WalkaboutSpr[ID].SetFrames(Int(spritenode.GetAttribute("frames")),Int(spritenode.GetAttribute("w")),Int(spritenode.GetAttribute("h")),"walkabout_f_"+ID)
								
								Local fnum:Int = 0
								For Local framenode:XMLElement = Eachin spritenode.Children
								
									Local csv:String[] = framenode.Value.Split(",")
									For Local i% = 0 Until csv.Length
										Local gid:Int = Int(csv[i].Trim())
										

										WalkaboutSpr[ID].frames[fnum].PixMap[i] = gid
										'rv.cells[i] = tileMap.CreateCell(gid, i Mod rv.width, i / rv.width)
									Next								
									
									'Print "Frame: "+fnum+" t: "+ID
									
									'Local bytes:Int[] = DecodeBase64Bytes(framenode.Value)
									
									'use databuffer method :/
									'Local db:DataBuffer = New DataBuffer(bytes.Length)
									'db.PokeBytes(0, bytes)
									'For Local i:int = 0 To WalkaboutSpr[tempi].frames[fnum].PixMap.Length-1
									
									'	WalkaboutSpr[tempi].frames[fnum].PixMap[i] = db.PeekInt( i*4 )
									
									'Next
									
									
									'rem
									'For Local i% = 0 Until bytes.Length
										' little endian
										'Local gid% = bytes[i]
										'gid += bytes[i + 1] Shl 8
										'gid += bytes[i + 2] Shl 16
										'gid += bytes[i + 3] Shl 24
										'MasterPalette.col[i / 4] = gid
										
										
									'Next								
									'end
									fnum = fnum + 1
								
								Next
								
								
								tempi = tempi + 1
						End Select
					
					Next
				Case "songs"
					Songs = New SongState
					Songs.SetMusicArray(Int(mtype.GetAttribute("count")))
					For Local songnode:XMLElement = Eachin mtype.Children
						Local bd:Int = Int(songnode.GetAttribute("id"))
						Songs.music[bd] = New MusicData
						Songs.music[bd].filename = songnode.GetAttribute("source")
						'Songs.music[bd].img = LoadImage(diddyGame.images.path + backdropnode.GetAttribute("source"))					
					
					next
			End select
		next
	
	End
	
	Method GetMapData:Void(datanode:XMLElement)
		Local TM:HTileMap[Int(datanode.GetAttribute("count"))]
		Local GM:MapData[Int(datanode.GetAttribute("count"))]
		For Local mapnode:XMLElement = Eachin datanode.Children
			
			GM[Int(mapnode.GetAttribute("id"))] = New MapData
			'For Local mapnode:XMLElement = Eachin mapsnode.Children
				'Local reader:MyTiledTileMapReader = New MyTiledTileMapReader
				'Print "Loading: "+mapnode.GetAttribute("name")
				'Local tmm:TileMap = reader.LoadMap(mapnode.GetAttribute("source"))
				'TM[Int(mapnode.GetAttribute("id"))] = HTileMap(tmm)
			GM[Int(mapnode.GetAttribute("id"))].Mapfile = mapnode.GetAttribute("source")
			'Next
			Local mapid:Int = Int(mapnode.GetAttribute("id"))	
				'grab door info
			For Local doorsnode:XMLElement = Eachin mapnode.Children
				'probably should have used a different name for this node..
				Select doorsnode.Name
				
					Case "npcs"
						Local nn:NPCType[Int(doorsnode.GetAttribute("count"))]
						GM[mapid].npcs = nn
						Local ni:Int = 0
						For Local npcnode:XMLElement = Eachin doorsnode.Children
						
							If GM[mapid].npcs[ni] = Null Then GM[mapid].npcs[ni] = New NPCType
							
							GM[mapid].npcs[ni].x = Int(npcnode.GetAttribute("x"))
							GM[mapid].npcs[ni].y = Int(npcnode.GetAttribute("y"))
							GM[mapid].npcs[ni].id = Int(npcnode.GetAttribute("id"))
							GM[mapid].npcs[ni].dir = Int(npcnode.GetAttribute("dir"))
							
							If GM[mapid].npcs[ni].dir = 2 Then GM[mapid].npcs[ni].dir = 4
							GM[mapid].npcs[ni].picture = Int(npcnode.GetAttribute("pic"))
							GM[mapid].npcs[ni].palette = Int(npcnode.GetAttribute("pal"))
							GM[mapid].npcs[ni].movetype = Int(npcnode.GetAttribute("mt"))
							GM[mapid].npcs[ni].speed = Int(npcnode.GetAttribute("spd"))

							GM[mapid].npcs[ni].textbox = Int(npcnode.GetAttribute("tb"))
							GM[mapid].npcs[ni].facetype = Int(npcnode.GetAttribute("ft"))
							GM[mapid].npcs[ni].item = Int(npcnode.GetAttribute("itm"))
							GM[mapid].npcs[ni].pushtype = Int(npcnode.GetAttribute("pt"))

							GM[mapid].npcs[ni].activation = Int(npcnode.GetAttribute("act"))
							GM[mapid].npcs[ni].tag1 = Int(npcnode.GetAttribute("tag1"))
							GM[mapid].npcs[ni].tag2 = Int(npcnode.GetAttribute("tag2"))
							GM[mapid].npcs[ni].usetag = Int(npcnode.GetAttribute("utg"))

							GM[mapid].npcs[ni].script = Int(npcnode.GetAttribute("scr"))
							GM[mapid].npcs[ni].scriptarg = Int(npcnode.GetAttribute("stg"))
							GM[mapid].npcs[ni].vehicle = Int(npcnode.GetAttribute("veh"))
							GM[mapid].npcs[ni].defaultzone = Int(npcnode.GetAttribute("dz"))
							GM[mapid].npcs[ni].defaultwallzone = Int(npcnode.GetAttribute("dwz"))

							
							ni+=1
						Next
				
					Case "doors"
						Local i:Int
						Local d:Door[Int(doorsnode.GetAttribute("count"))]
						Print "Door Count: "+doorsnode.GetAttribute("count")
						GM[Int(mapnode.GetAttribute("id"))].doors = d
						For Local doornode:XMLElement = Eachin doorsnode.Children
						
							If GM[Int(mapnode.GetAttribute("id"))].doors[i] = Null Then
								GM[Int(mapnode.GetAttribute("id"))].doors[i] = New Door
							Endif
							GM[Int(mapnode.GetAttribute("id"))].doors[i].x = Int(doornode.GetAttribute("x"))
							GM[Int(mapnode.GetAttribute("id"))].doors[i].y = Int(doornode.GetAttribute("y"))
							GM[Int(mapnode.GetAttribute("id"))].doors[i].bits[0] = Int(doornode.GetAttribute("bits"))
							i+=1
						Next
						
					Case "doorlinks"
						Local i:Int
						Local dl:DoorLink[Int(doorsnode.GetAttribute("count"))]
						GM[Int(mapnode.GetAttribute("id"))].links = dl
						For Local doorlinknode:XMLElement = Eachin doorsnode.Children
						
							If GM[Int(mapnode.GetAttribute("id"))].links[i] = Null Then
								GM[Int(mapnode.GetAttribute("id"))].links[i] = New DoorLink
							Endif
							GM[Int(mapnode.GetAttribute("id"))].links[i].source = Int(doorlinknode.GetAttribute("source"))
							GM[Int(mapnode.GetAttribute("id"))].links[i].dest = Int(doorlinknode.GetAttribute("dest"))
							GM[Int(mapnode.GetAttribute("id"))].links[i].dest_map = Int(doorlinknode.GetAttribute("dest_map"))
							GM[Int(mapnode.GetAttribute("id"))].links[i].tag1 = Int(doorlinknode.GetAttribute("tag1"))
							GM[Int(mapnode.GetAttribute("id"))].links[i].tag2 = Int(doorlinknode.GetAttribute("tag2"))
							

							i+=1
						Next				
				
				End Select
			
			Next
		Next
		GMap = GM
		Tilemaps = TM
		Return	
	End
	
	
	Method GetGenData:Void(datanode:XMLElement)
		
		 'change this so it uses old and new data arrays
		For Local gennode:XMLElement = Eachin datanode.Children
		
			Select gennode.Name
			
				Case "general"
					Local g:Int[Int(gennode.GetAttribute("count"))]
					For Local datnode:XMLElement = Eachin gennode.Children
					
						g[Int(datnode.GetAttribute("id"))] = Int(datnode.GetAttribute("val"))
						
					Next
					gdata = g
					CURRENTMAP = gdata[genStartMap]
				
					
					
				case "maps"
					GetMapData(gennode)
					Print "Load Maps"		
								
			End select
			
		
		Next
		
	
	End
	

End


Class MyTiledTileMapReader Extends TiledTileMapReader
	Method CreateMap:TileMap()
		Return New HTileMap
	End
End